package com.qf.controller;

import com.qf.entity.SysUser;
import com.qf.service.SysUserService;
import com.qf.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.UUID;

/**
 * --- 天道酬勤 ---
 *
 * @author QiuShiju
 * @date 2024/7/16
 * @desc 系统用户接口
 */
@RestController
@RequestMapping("/sys/user")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    /**
     * 系统用户登录
     * @param sysUser
     * @return token，session
     */
    @PostMapping("/login")
    public R login(@RequestBody SysUser sysUser, HttpSession session){
        SysUser login = sysUserService.login(sysUser);
        if (login != null) {
            //登陆成功存用户信息
            session.setAttribute("userInfo", login);
            //造假token
            HashMap<String,String> map = new HashMap<>();
            map.put("token", UUID.randomUUID().toString());
            return R.ok(map);
        }
        return R.fail();
    }

    /**
     * 获取用户信息
     * @param session
     * @return info
     */
    @GetMapping("/getInfo")
    public R getUserInfo(HttpSession session){
        SysUser Info = (SysUser)session.getAttribute("userInfo");
        return R.ok(Info);
    }

    /**
     * 获取用户信息
     * @param session
     * @return R
     */
    @GetMapping("/logout")
    public R logout(HttpSession session){
        //清除session
        session.removeAttribute("userInfo");
        System.out.println("用户已退出");
        return R.ok();
    }
}
